export default {
  name: 'SiderOption',
  props: {
  	initData:{
  		type:Array,
  		default () {
        return []
      }
  	},
    options: {
      type: Array,
      default () {
        return []
      }
    },
    btnText:{
    	type:String
    }
  },
  data () {
  	 return {
  	 	   downEnter:false,
  	 	   moveOld:'',
  	 	   moveType:'',
  	 	   hour:[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24],
  	 	   checkHour:[],
  	 	   borderline:[],
  	 	   allBtnSeleted:false
  	 }
  },
  watch:{
  	initData(val){
  		 this.checkHour=[];
  		 if(val.length==0)
  		 {
  		 	   this.checkHour=[];
  		 }else
  		 {
  		 	   for(var i=0;i<this.initData.length;i++)
  		 	   {
  		 	   	    if(this.initData[i]==true || this.initData[i]=='true'){
  		 	   	    	  this.checkHour.push(i);
  		 	   	    }
  		 	   }
  		 }
  		 this.changeData();
  		 this.sortCheck(); 
  	}
  },
  methods:{
  	down(ev){
  		   this.downEnter=true;
  		  if(!this.checkHour.includes(ev))
  		  {
  		  	this.checkHour.push(ev);
  		  	this.changeData();
  		  	this.moveType='add';
  		  	this.sortCheck();
  		  }else
  		  {
  		  	  this.checkHour.splice(this.checkHour.findIndex((n)=>n==ev),1);
  		  	  this.changeData();
  		  	  this.moveType='delete';
  		  	  this.sortCheck();
  		  }
  		  return false
  	},
  	move(ev){
  			if(this.downEnter){
  				 if(ev==this.moveOld)
  				 {
  				 	    return false;
  				 }
  				this.moveOld=ev;
					if(this.moveType=='add')
	  		  {
	  		  	if(!this.checkHour.includes(ev)){
  							this.checkHour.push(ev);
  							this.changeData();
	  		  			this.sortCheck();
  					}
	  		  
	  		  }else
	  		  {
	  		  	if(this.checkHour.includes(ev)){
  							this.checkHour.splice(this.checkHour.findIndex((n)=>n==ev),1);
  							this.changeData();
	  		  	  	this.sortCheck();
  					}
	  		  } 
  			}
  		  return false
  	},
  	sortCheck(){
  		  this.checkHour=this.checkHour.sort((a,b)=>{return a-b});
  		  this.checkHour.length==24?(this.allBtnSeleted=true):(this.allBtnSeleted=false);
  		  this.borderline=[];
  		  this.filtecheckHour();
  	},
  	filtecheckHour(_start)
  	{
  		   let start=_start || 0 ;
  		   this.borderline.push(this.checkHour[start]);
  		   for(var i=start;i<this.checkHour.length;i++)
  		   {
  		   	   if(this.checkHour[i+1]-this.checkHour[i]!=1)
  		   	   {
  		   	   	   this.borderline.push(this.checkHour[i]+1); 
  		   	   	   this.filtecheckHour(i+1);
  		   	   	   break;
  		   	   }
  		   };
  	},
  	changeData()
  	{
  			let arr=[]
  			for(var i=0;i<24;i++)
  			{
  				  if(this.checkHour.includes(i))
  				  {
  				  	  arr.push(true)
  				  }else{
  				  		arr.push(false)
  				  }
  			};
  			this.$emit('input', arr)
      		this.$emit('change', arr)
	  }
}
}
